import functools


def index_filter(index):
    """
    自定义过滤器，使点击排行新闻的前三个序号变颜色
    :return:
    """
    if index==1:
        return "first"
    elif index == 2:
        return "second"
    elif index==3:
        return "third"
    else:
        return ""

"""
自定义装饰器
"""
from flask import session,current_app,g
from info.models import User



def login_required(f):
    # @functools.wraps(f)
    def wrapper(*args,**kwargs):
        user_id = session.get("user_id", None)
        user = None
        if user_id:
            try:
                user = User.query.filter_by(id=user_id).first()
                # user = User.query.get(user_id)
                # print("=======",user)
            except Exception as e:
                current_app.logger.error(e)
        g.user = user
        return f(*args, **kwargs)
    # f.__name__ = wrapper.__name__
    wrapper.__name__ = f.__name__
    return wrapper


def status_filter(index):
    """
    自定义过滤器，使点击排行新闻的前三个序号变颜色
    :return:
    """
    if index==1:
        return "审核中"
    elif index == 0:
        return "审核通过"
    elif index== -1:
        return "审核未通过"
    else:
        return ""